local spEcho = Spring.Echo

function printTable (tableName, tableValue)
  local printedMap
  
  local function printTable2 (tableName, tableValue, depth)
    local indent = string.rep("  ", depth)
    
    if (type(tableValue) ~= "table") then
      if (type(tableValue) == "string") then tableValue = '"' .. tableValue .. '"'
      else tableValue = tostring(tableValue) end
      spEcho(indent .. tableName .. " = " .. tableValue)
    elseif (printedMap[tableValue]) then  -- avoid recursion / repetition
      spEcho(indent .. tableName .. " = { ALREADY PRINTED as " .. printedMap[tableValue] .. " }")
    else
  	  printedMap[tableValue] = tableName
      if (next(tableValue) == nil) then
	      spEcho(indent .. tableName .. " = {}")
      else
        spEcho(indent .. tableName .. " = {")

	      for name, value in pairs(tableValue) do
	        printTable2 (name, value, depth + 1)
	      end
        if (type(tableValue.pairs) == "function") then
	        spEcho(" ")
	        spEcho(indent .. "pairs():")      
          for name, value in tableValue:pairs() do
	          printTable2 (name, value, depth + 1)
	        end
        end
      
	      spEcho(indent .. "}") 
      end
    end	
  end  
  
  
  spEcho(" ")
  printedMap = {}
  printTable2 (tableName, tableValue, 0)
  spEcho(" ")  
end

--VFS.Include("luarules/utils/debug.lua")
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------